import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:nyh_motorcade_app/entity/company_entity.dart';
import 'package:nyh_motorcade_app/styles/colors.dart';
import 'package:nyh_motorcade_app/widgets/custom_app_bar.dart';
import 'package:nyh_motorcade_app/widgets/multi_state/multi_state_widget.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'select_account_controller.dart';

class SelectAccountPage extends StatelessWidget {
  final controller = Get.put(SelectAccountController());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colours.color_background,
      appBar: CustomAppBar(
        context: context,
        title: "选择账户",
        actions: [
          IconButton(
            onPressed: () {
              controller.searchAccount();
            },
            icon: Image.asset("images/ic_search_black.png"),
          ),
          IconButton(
            onPressed: () {
              controller.addAccount();
            },
            icon: Image.asset("images/ic_add_account.png"),
          ),
        ],
      ),
      body: GetBuilder<SelectAccountController>(
        builder: (controller) {
          return MultiStateWidget(
            child: ListView.builder(
              itemCount: controller.companyList.length,
              shrinkWrap: true,
              physics: BouncingScrollPhysics(),
              controller: controller.scrollController,
              itemBuilder: (context, index) {
                return buildItemWidget(index);
              },
            ),
            state: controller.multiState,
            onRetry: () {
              controller.onRetry();
            },
          );
        },
      ),
    );
  }

  Widget buildItemWidget(int index) {
    CompanyEntity companyEntity = controller.companyList[index];
    return Container(
      margin: EdgeInsets.only(left: 16.w, top: index == 0 ? 12.w : 0, right: 16.w, bottom: 12.w),
      decoration: BoxDecoration(
        color: Colours.color_white,
        borderRadius: BorderRadius.circular(10.w),
      ),
      child: ClipRRect(
        borderRadius: BorderRadius.circular(10.w),
        child: Material(
          child: InkWell(
            onTap: () {
              controller.backResult(companyEntity.id ?? "", companyEntity.name ?? "", companyEntity.code ?? "");
            },
            child: Padding(
              padding: EdgeInsets.all(16.w),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    companyEntity.name ?? "",
                    style: TextStyle(
                      color: Colours.color_text_theme,
                      fontSize: 16.sp,
                      fontWeight: FontWeight.bold,
                    ),
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                  ),
                  SizedBox(
                    height: 5.w,
                  ),
                  Text(
                    companyEntity.code ?? "",
                    style: TextStyle(
                      color: Colours.color_b1b2bb,
                      fontSize: 12.sp,
                    ),
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }
}
